package cn.xd.base.domain.entity;


import cn.xd.base.basic.type.*;
import cn.xd.base.domain.bean.command.AddAdminMenuCommand;
import cn.xd.base.domain.bean.command.UpdateAdminMenuCommand;
import cn.xd.core.basic.common.CreatorId;
import cn.xd.core.basic.common.UpdateTime;
import cn.xd.core.basic.common.UpdaterId;
import cn.xd.core.ddd.entity.Entity;
import lombok.Data;

/**
 * 菜单
 */
@Data
public class AdminMenu implements Entity<AdminMenuId> {
    private AdminMenuId id;
    private AdminMenuId parentId;
    private AdminMenuCode menuCode;
    private AdminMenuPerms perms;
    private AdminMenuRouter router;
    private AdminMenuType type;
    private AdminMenuIcon icon;
    private OrderNum orderNum;
    private Path viewPath;
    private MenuDisPlay display;
    private KeepAlive keepAlive;
    private CreatorId creatorId;
    private cn.xd.core.basic.common.CreateTime createTime;
    private UpdaterId updaterId;
    private UpdateTime updateTime;

    public AdminMenu() {
    }

    /**
     * 新增菜单
     *
     * @param command
     * @param userId
     * @return
     */
    public static AdminMenu add(AddAdminMenuCommand command, Long userId) {
        AdminMenu adminMenu = new AdminMenu();
        adminMenu.parentId = AdminMenuId.of(command.getParentId());
        adminMenu.menuCode = AdminMenuCode.of(command.getMenuCode());
        adminMenu.router = AdminMenuRouter.of(command.getRouter());
        adminMenu.perms = AdminMenuPerms.of(command.getPerms());
        adminMenu.type = AdminMenuType.of(command.getType());
        adminMenu.icon = AdminMenuIcon.of(command.getIcon());
        adminMenu.orderNum = OrderNum.of(command.getOrderNum());
        adminMenu.viewPath = Path.of(command.getViewPath());
        adminMenu.display = MenuDisPlay.of(command.getDisplay());
        adminMenu.keepAlive = KeepAlive.of(command.getKeepAlive());
        adminMenu.creatorId = CreatorId.of(userId);
        adminMenu.createTime = cn.xd.core.basic.common.CreateTime.now();
        adminMenu.updaterId = UpdaterId.of(userId);
        adminMenu.updateTime = UpdateTime.now();
        return adminMenu;
    }

    /**
     * 更新菜单
     *
     * @param command
     * @param userId
     */
    public void update(UpdateAdminMenuCommand command, Long userId) {
        this.parentId = AdminMenuId.of(command.getParentId());
        this.menuCode = AdminMenuCode.of(command.getMenuCode());
        this.router = AdminMenuRouter.of(command.getRouter());
        this.perms = AdminMenuPerms.of(command.getPerms());
        this.icon = AdminMenuIcon.of(command.getIcon());
        this.orderNum = OrderNum.of(command.getOrderNum());
        this.viewPath = Path.of(command.getViewPath());
        this.display = MenuDisPlay.of(command.getDisplay());
        this.keepAlive = KeepAlive.of(command.getKeepAlive());
        this.updaterId = UpdaterId.of(userId);
        this.updateTime = UpdateTime.now();
    }
}
